package com.dfrz.household.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dfrz.household.bean.Server;
import com.dfrz.household.bean.ServerPerson;
import com.dfrz.household.bean.ShopCart;
import com.dfrz.household.mapper.ShopCartMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class ShopCartServiceImpl implements IShopCartService {

    @Autowired
    ShopCartMapper mapper;
    @Autowired
    ServerPersonService serverPersonService;
    @Autowired
    ServerService serverService;
    @Override
    public int shopCart_insert(ShopCart cart) {
        int count=mapper.insert(cart);
        return count;
    }

    @Override
    public ShopCart getCartById(Integer id) {
        ShopCart cart = new ShopCart();
        cart=mapper.selectById(id);
        return cart;
    }

    @Override
    public List<ShopCart> getShopCartsByUid(Integer uid) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("uid",uid);
        List<ShopCart> list=mapper.selectList(queryWrapper);
        for (ShopCart shopCart : list) {
            Server server =serverService.getServerById(shopCart.getSdid());
            ServerPerson serverPerson=serverPersonService.getUserById(server.getSerid());
            shopCart.setServerPerson(serverPerson);
            shopCart.setServer(server);
        }
        return list;
    }

    @Override
    public int deleteShopCart(Integer id) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("id",id);
        int count=mapper.delete(queryWrapper);
        return count;
    }

    @Override
    public int updateShopCart(ShopCart cart) {
        int count=mapper.updateById(cart);
        return count;
    }
}
